<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */
class AppController extends Controller {
    var $uses=array('User');
	var $components = array('Auth','Session');

   function beforeFilter(){
			Security::setHash("md5");
        	$this->Auth->authenticate = ClassRegistry::init('User');
            $user = $this->Auth->user();
            
            if(empty($user) && $this->params['action'] != 'login'){
			$this->redirect(array('controller' => 'users', 'action' => 'login'));
		      }
       	$this->Auth->loginAction 	= array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect 	= array('controller'=>'pages', 'action' => 'home');
		$this->Auth->loginError 	= 'Tên tài khoản hoặc mật khẩu sai';
		$this->Auth->authError 		= 'Bạn cần phải đăng nhập hệ thống để sử dụng các chức năng';
		$this->Auth->logoutRedirect = '/';    
   }
    
  
  function beforeRender() {
        // Tạo biến curUser trong session
        $curUser=$this->Auth->user();
		$group_id = $curUser['User']['group_id'];
		//echo $group_id;
		//debug($curUser);
        if(count($curUser)>0){
			switch ($group_id){
			case 1 :
				$hoten = $this->User->query("SELECT nhatuyendungs.tenntd, nhatuyendungs.id,nhatuyendungs.dienthoai,nhatuyendungs.emailntd,nhatuyendungs.diachintd
                        from nhatuyendungs, users
                            where group_id = '1'
								and users.id=".$curUser['User']['id']);
								
				//echo $curUser['User']['id']; 
				$quyen= $this->User->query("SELECT phanquyens.role_id
                                       from phanquyens, nhatuyendungs, users
                                       where phanquyens.group_id = users.group_id and users.id=".$curUser['User']['id']);
            
				$ten = $hoten[0]['nhatuyendungs']['tenntd'];
                $email = $hoten[0]['nhatuyendungs']['emailntd'];
                $dienthoai = $hoten[0]['nhatuyendungs']['dienthoai'];
                $diachi = $hoten[0]['nhatuyendungs']['diachintd'];
				$this->Session->write('cur_ten',$ten);
				$ho_ten=$this->Session->read('cur_ten');
				
				$curid = $curUser['User']['id'];
				$this->Session->write('cur_id',$curid);
				$cur_id=$this->Session->read('cur_id');
				
				$this->Session->write('cur_role',$quyen);
				$role_id=$this->Session->read('cur_role');
				break;
			 case 2 :
				$hoten = $this->User->query("SELECT ungviens.hoten, ungviens.id,ungviens.emailuv,ungviens.dienthoaiuv,ungviens.noiohientai
                        from ungviens, users
                            where group_id = '2'
								and users.id=".$curUser['User']['id']);
                 
				$quyen= $this->User->query("SELECT phanquyens.role_id
                                        from phanquyens, ungviens, users
                                       where phanquyens.group_id = users.group_id and users.id=".$curUser['User']['id']);
				
				$ten = $hoten[0]['ungviens']['hoten'];
                $email = $hoten[0]['ungviens']['emailuv'];
                $dienthoai = $hoten[0]['ungviens']['dienthoaiuv'];
                $diachi = $hoten[0]['ungviens']['noiohientai'];
				$this->Session->write('cur_ten',$ten);
				$ho_ten=$this->Session->read('cur_ten');
				
				$curid = $curUser['User']['id'];
				$this->Session->write('cur_id',$curid);
				$cur_id=$this->Session->read('cur_id');
				
				$this->Session->write('cur_role',$quyen);
				$role_id=$this->Session->read('cur_role');
				break;
			case 3 :
				$hoten = $this->User->query("SELECT users.username, users.id
                        from users
                            where group_id = '3'
								and users.id=".$curUser['User']['id']);
                   
				$quyen= $this->User->query("SELECT phanquyens.role_id
                                        from phanquyens,users
                                       where phanquyens.group_id = users.group_id and users.id=".$curUser['User']['id']);
            
				$ten = $hoten[0]['users']['username'];
				$this->Session->write('cur_ten',$ten);
				$ho_ten=$this->Session->read('cur_ten');
				
				$curid = $curUser['User']['id'];
				$this->Session->write('cur_id',$curid);
				$cur_id=$this->Session->read('cur_id');
				
				$this->Session->write('cur_role',$quyen);
				$role_id=$this->Session->read('cur_role');
				break;
			} 
        }
     
        $this->set(compact('curUser','hoten','ho_ten','cur_id','quyen','role_id','group_id','ten','email','dienthoai','diachi'));
	}
   
   
   
   var $paginate = array(
        'Nhatuyendung'=>array(
            'limit' => 20,
            ),
        'Ungvien'=>array(
            'limit' => 20,
            ),
        'User'=>array(
            'limit' => 20,
            ),
        'Dangky'=>array(
            'limit' => 20,
            'order' => array(
            'Dangky.ngaydang' => 'desc')
            ),
        'Tinhtp'=>array(
            'limit' => 20,
            ),
        'Chuyenmon'=>array(
            'limit' => 20,
            ),
  ); 
    

}
?>